#include "screenshot.h"
#include <QApplication>
#include <QStyleOption>
#include <QPainter>
#include <QPen>
#include <QMouseEvent>
#include <QClipboard>

ScreenShot::ScreenShot(QScreen *screen, QPoint startPoint, QPoint endPoint, WId windowId, int index, QWidget *parent):QDialog(parent)
{
    this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
    this->screen = screen;
    this->startPoint = startPoint;
    this->endPoint = endPoint;
    this->windowId = windowId;
    this->index = index;

    fullScreenButton = new QPushButton("确认");
    fullScreenButton->setStyleSheet(
        "QPushButton {"
        "  border: 2px solid #8FBC8F;"
        "  border-radius: 10px;"
        "  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E0FFFF, stop: 1 #8FBC8F);"
        "  color: #000000;"
        "  padding: 5px;"
        "  font-size:24px;"
        "  letter-spacing:3px; font-weight: 900; "
        "}"
        "QPushButton:hover {"
        "  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #C0FF3E, stop: 1 #87CEEB);"
        "}"
        "QPushButton:pressed {"
        "  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #7FFFD4, stop: 1 #32CD32);"
        "}"
        );
    fullScreenButton->setGeometry(screen->size().width()/2 -50, 40, 100, 40);
    fullScreenButton->setParent(this);
    connect(fullScreenButton, &QPushButton::clicked, this, &ScreenShot::confirmRect);

}

void ScreenShot::showEvent(QShowEvent *event)
{
    fullScreenBack = screen->grabWindow(windowId, 0, 0, screen->size().width(), screen->size().height());
    QPixmap mask(screen->size().width(), screen->size().height());
    mask.fill(QColor(255, 255, 0, 80));
    fullScreenDisplay = fullScreenBack;
    QPainter painter(&fullScreenDisplay);
    painter.drawPixmap(0, 0, mask);
}

void ScreenShot::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    QPen pen;
    pen.setWidth(2);
    pen.setColor(Qt::darkGreen);
    pen.setStyle(Qt::DashDotLine);
    p.setPen(pen);
    p.drawPixmap(0, 0, fullScreenDisplay);

    if(isPressed)
    {
        QRect rect(getCapturedRect());
        if (rect.isValid())
        {
            p.drawPixmap(rect.x(),rect.y(),fullScreenBack.copy(rect));
            p.drawRect(rect);
        }
        // QBrush brush;
        // brush.setColor(Qt::darkGreen);
        // brush.setStyle(Qt::FDiagPattern);
        // p.setBrush(brush);
        // p.drawRect(rect);
    }
}

void ScreenShot::mousePressEvent(QMouseEvent *event)
{
    if (event->button()==Qt::LeftButton)
    {
        isPressed = true;
        startPoint = endPoint = event->pos();
        this->update();
    }
}

void ScreenShot::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressed)
    {
        endPoint = event->pos();
        this->update();
    }
}

void ScreenShot::mouseReleaseEvent(QMouseEvent *event)
{
    isPressed = false;
    if (qAbs(endPoint.x() - startPoint.x()) < 100 || qAbs(endPoint.y() - startPoint.y()) < 50)
    {
        startPoint = endPoint = event->pos();
        this->update();
    }
    // QRect rect(startPoint, endPoint);
    // qDebug()<<"选择的范围："<<rect;
    // QScreen *screen = QApplication::primaryScreen();
    // QPixmap pximap = screen->grabWindow(0, rect.x(),rect.y(),rect.width(),rect.height());
    // QImage image = pximap.toImage();
    // image.save("D:\\cC.jpg");
    // close();
}

void ScreenShot::mouseDoubleClickEvent(QMouseEvent *event)
{
    // 双击会触发单击事件，不做处理
    // this->confirmRect();
}

QRect ScreenShot::getCapturedRect()
{
    QRect ret;
    ret.setLeft(qMin(startPoint.x(), endPoint.x()));
    ret.setTop(qMin(startPoint.y(), endPoint.y()));
    ret.setWidth(qAbs(endPoint.x()-startPoint.x()));
    ret.setHeight(qAbs(endPoint.y()-startPoint.y()));
    return ret;
}

void ScreenShot::confirmRect()
{
    QRect rect(getCapturedRect());
    if (rect.isValid())
    {
        emit rectConfirmed(rect, this->index);
    }
    // QClipboard *clipboard = QApplication::clipboard();
    // clipboard->setPixmap(fullScreenBack.copy(rect));
    this->hide();
}
